package com.lxc.domain.manage.service;

import com.lxc.domain.manage.model.aggregates.ApplicationSystemRichInfo;
import com.lxc.domain.manage.model.entity.*;
import com.lxc.domain.manage.repository.IConfigManageRepository;
import com.lxc.types.common.Constants;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxc18
 * @date 2024/12/14 21:02
 * @description ConfigManageServiceImpl
 */
@Service
public class ConfigManageServiceImpl implements IConfigManageService{

    @Resource
    IConfigManageRepository configManageRepository;
    @Override
    public List<GatewayServerEntity> queryGatewayServerList() {
        return configManageRepository.queryGatewayServerList();

    }

    @Override
    public boolean registerGatewayServerNode(String groupId, String gatewayId, String gatewayName, String gatewayAddress) {
        GatewayServerDetailEntity gatewayServerDetailEntity = configManageRepository.queryGatewayServerDetail(gatewayId, gatewayAddress);
        if (null == gatewayServerDetailEntity) {
            try {
                return configManageRepository.registerGatewayServerNode(groupId, gatewayId, gatewayName, gatewayAddress, Constants.GatewayStatus.Available);
            } catch (DuplicateKeyException e) {
                return configManageRepository.updateGatewayStatus(gatewayId, gatewayAddress, Constants.GatewayStatus.Available);
            }
        } else {
            return configManageRepository.updateGatewayStatus(gatewayId, gatewayAddress, Constants.GatewayStatus.Available);
        }

    }

    @Override
    public ApplicationSystemRichInfo getAllInfoByGatewayId(String gatewayId,String systemId) {

        ApplicationSystemRichInfo result = new ApplicationSystemRichInfo();
        result.setGatewayId(gatewayId);
        //首先活动system_list列表，一个gatedwayId，多个system；一个system多个interface；一个interface，一个method
        List<String> systemIdList = new ArrayList<String>();
        if(null==systemIdList){
            systemIdList.add(systemId);
        }
        else{
            systemIdList = configManageRepository.getGatewayDistributionListByGatewayId(gatewayId);
        }

        ArrayList<ApplicationSystemEntity> applicationSystemEntities = new ArrayList<>(systemIdList.size());
        for(String id:systemIdList){
            ApplicationSystemEntity system = configManageRepository.getApplicationSystemBySystemId(id);
            if(system==null){
                continue;
            }
            //从system获取interfaceId,
            List<ApplicationInterfaceEntity> interfaceEntityList = configManageRepository.getApplicationInterfaceBySystemId(id);
            for(ApplicationInterfaceEntity interfaceEntity:interfaceEntityList){
                //一个interface，一个method
                List<ApplicationInterfaceMethodEntity> methods = configManageRepository.getMethodsByInterfaceId(interfaceEntity.getInterfaceId());
                interfaceEntity.setInterfaceMethodEntityList(methods);
            }
            system.setInterfaceEntityList(interfaceEntityList);
            applicationSystemEntities.add(system);

        }
        result.setApplicationSystemEntities(applicationSystemEntities);
        return result;
    }

    @Override
    public List<GatewayServerDetailEntity> queryGatewayServerDetailList() {
        return configManageRepository.queryGatewayServerDetailList();
    }
}
